package com.neusoft.elm.filter;

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/*")
public class CorsFilter implements Filter{

	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		
		/* 关于"Access-Control-Allow-Origin"设置
		 * 设置只允许http://localhost:8081进行跨域访问
		 * 注意此处还需修改，因为服务器端使用的是8080端口，客户端不能与服务器端使用相同端口
		 * 当前服务器端与客户端使用同一台主机，所以都使用localhost，后续需要更改
		 * 想要允许发送cookie，则不能将其设置为"*"
		 */
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Cotrol-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTINOS,DELETE,PUT");
		response.setHeader("Access-Control-Max-Age", "3628800");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
		filterChain.doFilter(servletRequest, servletResponse);
		
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {}

}
